package com.niit.dao;

import com.niit.pojo.StudentTable;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

/**
 * @author 杨铭
 * 2022/2/7,20:46
 */

public class addStudent {
    Configuration conf;

    public addStudent() {
        HbaseConnection hbaseConnection = new HbaseConnection();
        Configuration conf = hbaseConnection.connect();
        this.conf = conf;
    }

    public int add(StudentTable studentTable) throws IOException {
        HTable hTable = new HTable(conf, "Student_Table");
        int x = 0;
        Scan scan = new Scan();
        ResultScanner rs = hTable.getScanner(scan);
        for (Result r : rs) {
            if (Bytes.toString(r.getRow()).equals(studentTable.getStudentId())) {
                x = 1;
                break;
            }
        }
        if (x == 0) {
            Put put = new Put(Bytes.toBytes(studentTable.getStudentId()));   //ROW KEY
            put.add(Bytes.toBytes("info"), Bytes.toBytes("Name"), Bytes.toBytes(studentTable.getName()));
            put.add(Bytes.toBytes("info"), Bytes.toBytes("DateOfEnrollment"), Bytes.toBytes(studentTable.getDateOfEnrollment()));
            put.add(Bytes.toBytes("info"), Bytes.toBytes("ContactNumber"), Bytes.toBytes(studentTable.getContactNumber()));
            put.add(Bytes.toBytes("status"), Bytes.toBytes("Semester"), Bytes.toBytes(studentTable.getSemester()));
            put.add(Bytes.toBytes("status"), Bytes.toBytes("StudentStatus"), Bytes.toBytes("L"));
            hTable.put(put);
        }
        hTable.close();
        return x;
    }
}
